﻿using System.Web.Mvc;
using System.Web.Routing;
using mBlog.Infrastructure;
using mBlog.Core;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Configuration;
using mBlog.Infrastructure.AutoMapper;
using mBlog.Infrastructure.Filters;

namespace mBlog
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new ElmahHandlerErrorAttribute());
            //filters.Add(new CompressAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add(
                    new Route("{XmlRpc}", null,
                        new RouteValueDictionary(new { XmlRpc = "XmlRpc" }),
                        new MetaWeblogRouteHandler()));
            routes.MapRoute(
                "RSS",
                "Rss",
                new { controller = "API", action = "RSS" },
                new[] { "mBlog.Controllers" }
                );


            routes.MapRoute(
                "Archives",
                "Archives/{year}/{month}",
                new { controller = "Archive", action = "Index" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "ViewCategory",
                "Categories/{urlName}",
                new { controller = "Category", action = "View" },
                new[] { "mBlog.Controllers"}
                );

            routes.MapRoute(
                "ViewTag",
                "Tags/{urlName}",
                new { controller = "Tag", action = "View" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "AddComment",
                "Post/AddComment",
                new { controller = "Post", action = "AddComment" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "ViewPost",
                "posts/{urlName}",
                new { controller = "Post", action = "View" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "HomePager",
                "pages/{page}",
                new { controller = "Home", action = "Page" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "Home",
                "",
                new { controller = "Home", action = "Index" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "Logon",
                "Logon",
                new { controller = "Home", action = "Logon" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "Footer",
                "Footer",
                new { controller = "Home", action = "Footer" },
                new [] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "NotFound",
                "NotFound",
                new { controller = "Home", action = "NotFound" },
                new[] { "mBlog.Controllers" }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["mBlog"].ConnectionString);
            //var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
            //Database.DefaultConnectionFactory = profiled;

            DependencyResolver.SetResolver(new mBlogDependencyResolver());

            new AutoMapperConfiguration().Init();
        }
    }
}
